//
//  FTSelFriendViewCell.m
//  FaceTo
//
//  Created by 姚冬元 on 2017/8/10.
//  Copyright © 2017年 chengbangming. All rights reserved.
//

#import "FTSelFriendViewCell.h"

@implementation FTSelFriendViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self initUI];
    }
    return self;
}

- (void)initUI
{
    _isSelected = NO;
    
    _selectBtn = [[UIButton alloc] init];
    [_selectBtn setImage:[UIImage imageNamed:@"choose"] forState:UIControlStateNormal];
    [_selectBtn addTarget:self action:@selector(chooseClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_selectBtn];
    
    _avatar = [[UIImageView alloc] init];
//    _avatar.image = [UIImage imageNamed:@"Icon-120"];
    [self addSubview:_avatar];
    
    _namelbl = [[UILabel alloc] init];
    _namelbl.text = @"好友昵称";
    _namelbl.textColor = FontColor;
    _namelbl.font = [UIFont systemFontOfSize:15];
    [self addSubview:_namelbl];
    
    _scorelbl = [[UILabel alloc] init];
    _scorelbl.text = @"积分";
    _scorelbl.textColor = FontColor;
    _scorelbl.font = [UIFont systemFontOfSize:14];
    [self addSubview:_scorelbl];
    
    [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.equalTo(self);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    [_avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_selectBtn.mas_right).with.offset(15);
        make.centerY.equalTo(self);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];
    
    [_namelbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatar.mas_right).with.offset(15);
        make.centerY.equalTo(self);
    }];
    
    [_scorelbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.centerY.equalTo(self);
    }];
    
    // 头像裁圆
    UIImage *image = [UIImage imageNamed:@"Icon-120"];
    //1.开启一个位图上下文(大小跟图片一样大)
    UIGraphicsBeginImageContext(image.size);
    //2.做裁剪.(对之前已经画上去的东西,不会有做用.)
    //2.1 bezierPathWithOvalInRect方法后面传的Rect,可以看作(x,y,width,height),前两个参数是裁剪的中心点,后面两个决定裁剪的区域是圆形还是椭圆.
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //把路径设置为裁剪区域(超出裁剪区域以外的内容会自动裁剪掉.)
    [path addClip];
    //3.把图片绘制到上下文当中
    [image drawAtPoint:CGPointZero];
    //4.从上下文当中生成一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.关闭上下文
    UIGraphicsEndImageContext();
    //6.把图片显示出来
    _avatar.image = newImage;
}

- (void)chooseClick
{
    _isSelected = !_isSelected;
    
    _isSelected ? [_selectBtn setImage:[UIImage imageNamed:@"check2"] forState:UIControlStateNormal] : [_selectBtn setImage:[UIImage imageNamed:@"choose"] forState:UIControlStateNormal];
    
    if (_block) {
        _block(_isSelected);
    }
}
@end
